package com.vincent.kdp.Activity;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.vincent.baseapp.util.view.LoadingView;
import com.vincent.kdp.R;
import com.vincent.kdp.adapter.HourseListAdapter;
import com.vincent.kdp.bean.JudgeBean;
import com.vincent.kdp.bean.Paginator;
import com.vincent.kdp.bean.WareHouseGoodsBean;
import com.vincent.kdp.http.RequestCallBack;
import com.vincent.kdp.ui.decoration.DividerItemDecoration;
import com.vincent.kdp.ui.fragment.repository.CommodityRepository;
import com.vincent.kdp.util.BaseParam;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WareHouseGoodsActivity extends AppCompatActivity {
    @Bind(R.id.back_img)
    ImageView ivBack;
    @Bind(R.id.house_recy)
    RecyclerView houseRecy;
    @Bind(R.id.SwipeRefreshLayout_house)
    SwipeRefreshLayout swipeRefreshLayout;
    @Bind(R.id.toolbar_center_tv)
    TextView toolBarTv;


    public  boolean hasNext=true;
    private String num;
    private String id;
    CommodityRepository repository=new CommodityRepository();
    ArrayList<WareHouseGoodsBean.HouseData.HouseGoodsList> list=new ArrayList<>();
    private HourseListAdapter adapter;
    private WareHouseGoodsActivity thisActivity;
    private int pageNum = 1;//请求页数，默认请求第一条
    boolean isLoading;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ware_house_goods);
        ButterKnife.bind(this);
        initView();
        initData();
    }
    private void initView() {
        thisActivity=this;
        Intent intent=getIntent();
        num=intent.getStringExtra("cStyleNum");
        id=intent.getStringExtra("departId");
        toolBarTv.setText("库存列表");

        swipeRefreshLayout.setProgressViewOffset(false, 70, (int) TypedValue
                .applyDimension(TypedValue.COMPLEX_UNIT_DIP, 90, getResources()
                        .getDisplayMetrics()));
        swipeRefreshLayout.setColorSchemeResources(R.color.black);
        swipeRefreshLayout.post(new Runnable() {
            @Override
            public void run() {
                swipeRefreshLayout.setRefreshing(true);
            }
        });
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                initData();
            }
        });

        final LinearLayoutManager manager=new LinearLayoutManager(this);
        houseRecy.setLayoutManager(manager);
        houseRecy.addItemDecoration(new DividerItemDecoration(WareHouseGoodsActivity.this,LinearLayoutManager.VERTICAL));

        houseRecy.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                int lastVisibleItemPosition = manager.findLastVisibleItemPosition();
                if (lastVisibleItemPosition + 1 == adapter.getItemCount()) {
                    if(thisActivity.hasNext){
                        boolean isRefreshing = swipeRefreshLayout.isRefreshing();
                        if (isRefreshing) {
                            adapter.notifyItemRemoved(adapter.getItemCount());
                            return;
                        }
                        initData();
                    }else{

                    }
                }
            }
        });
        adapter=new HourseListAdapter(this,list);

        houseRecy.setAdapter(adapter);

        adapter.setHouseItemClickListener(new HourseListAdapter.HouseItemClickListener() {
            @Override
            public void itemClick(View v, WareHouseGoodsBean.HouseData.HouseGoodsList data) {
                Log.i("code",data.getName()+"----"+data.getBarCode());
                //需要判断是否是本店的商品
                judgeTurn(data.getBarCode());

            }
        });
    }

    private void judgeTurn(final String barCode) {
        final LoadingView load=new LoadingView(this);
        load.show();
        repository.getGoodsTurn(barCode, new RequestCallBack() {
            @Override
            public void onSuccess(Object object) {
                JudgeBean bean= (JudgeBean) object;
                if("1".equals(bean.getData().getIsShow())){
                    //1跳到申请调货
                    Intent intent=new Intent(WareHouseGoodsActivity.this,GetGoodsDetailActivity.class);
                    BaseParam.setCurrentBarCode( barCode);
                    intent.putExtra("barCode", barCode);
                    startActivity(intent);
                }else if("0".equals(bean.getData().getIsShow())){
                    //0调到商品详情barCode
                    Intent intent=new Intent(WareHouseGoodsActivity.this,GoodsDetailActivity.class);
                    BaseParam.setCurrentBarCode(barCode);
                    intent.putExtra("barCode", barCode);
                    startActivity(intent);
                }

                load.dismiss();
            }

            @Override
            public void onFalse(String msg) {
                load.dismiss();
            }
        });
    }

    private void initData() {
        repository.reqDifferentHouseList(pageNum+"",num, id, new RequestCallBack() {
            @Override
            public void onSuccess(Object object) {
                WareHouseGoodsBean bean= (WareHouseGoodsBean) object;
                WareHouseGoodsBean.HouseData.HousePaginator paginator=bean.getData().getPaginator();
                if(bean.getData().getList().size()==0){

                }else if(thisActivity.hasNext){
                    list.addAll(bean.getData().getList());
                    swipeRefreshLayout.setRefreshing(false);
                    adapter.notifyItemRemoved(adapter.getItemCount());
                    thisActivity.pageNum=thisActivity.pageNum+1;
                    thisActivity.hasNext=paginator.isHasNextPage();
                    adapter.notifyDataSetChanged();
                }else{
                    Toast.makeText(thisActivity,"没有更多数据",Toast.LENGTH_SHORT).show();
                }
                swipeRefreshLayout.setRefreshing(false);
            }

            @Override
            public void onFalse(String msg) {
                Toast.makeText(thisActivity,"暂无数据",Toast.LENGTH_SHORT).show();
                swipeRefreshLayout.setRefreshing(false);
            }
        });
    }

    @OnClick(R.id.back_img)
    public void onClick(){
        this.finish();
        overridePendingTransition(com.vincent.baseapp.R.anim.fragment_left_in, com.vincent.baseapp.R.anim.fragment_right_out);
    }
}
